package com.woniu.order.listener;

import com.alibaba.fastjson2.JSONObject;
import com.rabbitmq.client.Channel;
import com.woniu.common.dto.UserDto;
import com.woniu.common.utils.UserUtil;
import com.woniu.order.mapper.ZsbJwtMapper;
import com.woniu.order.pojo.ZsbJwt;
import com.woniu.order.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OrderApplicationListener {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ZsbJwtMapper zsbJwtMapper;
    @Autowired
    private WebSocketServer webSocketServer;

    @RabbitListener(queues = "dlx.ecp.ttl.queue")
    public void listener(String msg, Message message, Channel channel) {
        log.info("销售申请进入死信");
        String token = message.getMessageProperties().getMessageId();
        if (redisTemplate.delete(token)) {
            JSONObject jsonObject = JSONObject.parseObject(msg);
            String orderNum = jsonObject.getString("orderNum");
            String approver = jsonObject.getString("approver");
            ZsbJwt zsbJwt = zsbJwtMapper.queryJwtZsbJwt();
            UserDto user = UserUtil.getUser(zsbJwt.getJwt());
            if (approver.equals(user.getId().toString())){
                webSocketServer.sendToAllClient(orderNum);
            }
        }

    }


}
